# 要下载模型文件

import random
from towhee import AutoPipes
from towhee import pipe
from towhee import pipe, ops
from towhee.datacollection import DataCollection

def demo0():
    p = AutoPipes.pipeline('sentence_embedding')
    output = p('hello world').get()
    print(output)

def demo1():
    p = pipe.input('num').output('num')

    res = p(1)
    DataCollection(res).show()

def demo2():
    p = pipe.input('num').map('num','num',lambda x: x*3).output('num')

    res = p(1)
    DataCollection(res).show()
    
def demo3():
    p = pipe.input('num').flat_map('num','num',lambda x: [i*2 for i in x]).output('num')

    res = p([1,2,3])
    DataCollection(res).show()
    
def demo4():
    p = pipe.input('num').flat_map('num','num',lambda x: [i*2 for i in x]).filter('num','num','num',lambda x:x>2).output('num')

    res = p([1,2,3])
    DataCollection(res).show()

def demo5():
    p = pipe.input('num').flat_map('num','num',lambda x: [i*2 for i in x]).window('num','nums',2,1,lambda x:[i*2 for i in x]).output('num','nums')
    res = p([1,2,3,4])
    DataCollection(res).show()

def demo6():
    text_embedding = (pipe.input('text')
         .map('text', 'embedding', ops.sentence_embedding.transformers(model_name='all-MiniLM-L6-v2'))
         .output('text', 'embedding')
     )

    data = ['Hello, world.', 'How are you?']
    res = text_embedding.batch(data)   
    for item in res:
        DataCollection(item).show()

def demo7():
    obj_embedding = (
        pipe.input('url')
            .map('url', 'img', ops.image_decode.cv2_rgb())
            .flat_map('img', ('box', 'class', 'score'), ops.object_detection.yolo())
            .flat_map(('img', 'box'), 'object', ops.towhee.image_crop())
            .map('object', 'embedding', ops.image_embedding.timm(model_name='resnet50'))
            .output('url', 'object', 'class', 'score', 'embedding')
        )


    data = 'https://towhee.io/object-detection/yolo/raw/branch/main/objects.png'
    res = obj_embedding(data)
    res.size # return 2
    DataCollection(res).show()

def demo8():

    obj_filter_embedding = (
        pipe.input('url')
            .map('url', 'img', ops.image_decode.cv2_rgb())
            .map('img', 'obj_res', ops.object_detection.yolo())
            .filter(('img', 'obj_res'), ('img', 'obj_res'), 'obj_res', lambda x: len(x) > 0)
            .flat_map('obj_res', ('box', 'class', 'score'), lambda x: x)
            .flat_map(('img', 'box'), 'object', ops.towhee.image_crop())
            .map('object', 'embedding', ops.image_embedding.timm(model_name='resnet50'))
            .output('url', 'object', 'class', 'score', 'embedding')
        )
        
    data = ['https://towhee.io/object-detection/yolo/raw/branch/main/objects.png', 'https://github.com/towhee-io/towhee/raw/main/assets/towhee_logo_square.png']
    res = obj_filter_embedding.batch(data)    
 
def demo9():

    video_frame_embedding = (
        pipe.input('url')
            .flat_map('url', 'frame', ops.video_decode.ffmpeg())
            .window('frame', 'frame', 10, 10, lambda x: x[random.randint(0, len(x)-1)])
            .map('frame', 'embedding', ops.image_embedding.timm(model_name='resnet50'))
            .output('url', 'frame', 'embedding')
    )

    data = 'https://raw.githubusercontent.com/towhee-io/examples/0.7/video/reverse_video_search/tmp/Ou1w86qEr58.gif'
    data = 'https://vdn6.vzuu.com/SD/f2ee15d2-5e7a-11ed-8b67-6aabf7a6cc65.mp4?pkey=AAUIMh1lmGnh-Z_u6EwAmdcfoNZy1Qtpo4ZV2L78CdVZErvmKmpcz92Aqd0kF0GPrYjhWbgC0f57kQPkcw47xSck&c=avc.1.1&f=mp4&pu=078babd7&bu=078babd7&expiration=1697763968&v=ks6'
    res = video_frame_embedding(data)      
    
demo6()



   
